package api_day04;

/*
 * 数据结构之顺序结构实现集合功能
 * @author Dalwin
 * @date 2022.7.27
 */
public class MyArrayList {
    //Object类数组用于存储数据
    Object[] objects;
    //表示集合长度
    private int length;
    //表示集合元素个数
    private int size;

    //无参构造方法
    public MyArrayList() {
        length = 1;
        objects = new Object[length];
        size = 0;
    }
    //有参构造方法
    public MyArrayList(int length) {
        this.length = length;
        objects = new Object[length];
        this.size = 0;
    }
    //数组扩容方法
    private Object[] grow(){
        Object[] copy = new Object[length +1];
        for (int i = 0; i < length; i++) {
            copy[i] = objects[i];
        }
        length++;
        return copy;
    }
    //add方法，向集合(this)中末尾添加元素
    public void add(Object object){
        if (size < length) objects[size] = object;
        if (isFull()){
            objects = grow();
            objects[size] = object;
        }
        size++;
    }
    //insert方法，向集合index的位置中插入obj元素
    public void insert(int index,Object object){
        if (outOfBound(index)) return;
        if (size < length) {
            for (int i = size; i > index ; i--) {
                objects[i] = objects[i-1];
            }
            objects[index] = object;
            size++;
        }else {
            objects = grow();
            for (int i = size; i > index ; i--) {
                objects[i] = objects[i-1];
            }
            objects[index] = object;
            size++;
        }
    }
    //替换指定位置上的元素,并且返回被替换的元素
    public Object replace(int index,Object object){
        if (outOfBound(index)) return null;
        Object o = objects[index];
        objects[index] = object;
        return o;
    }
    //从集合中获得指定位置的元素
    public Object get(int index){
        if (outOfBound(index)) return null;
        return objects[index];
    }
    //删除指定位置(index)上的元素，并且返回删除的元素
    public Object remove(int index){
        if (outOfBound(index)) return null;
        Object o = objects[index];
        for (int i = index; i < size - 1; i++) {
            objects[i] = objects[i+1];
        }
        size--;
        return o;
    }
    //删除第一个指定元素(obj)
    public boolean remove(Object object){
        if (!contain(object)) return false;
        else {
            for (int i = 0; i < size; i++) {
                if (objects[i].equals(object)){
                    remove(i);
                    break;
                }
            }
            return true;
        }
    }
    //获得集合中的元素个数
    public int size(){
        return size;
    }
    //判断集合中是否存在指定元素obj
    public boolean contain(Object object){
        for (int i = 0; i < size; i++) {
            if (objects[i].equals(object)){
                return true;
            }
        }
        return false;
    }
    //判空方法
    public boolean isEmpty(){
        return size == 0;
    }
    //判断下标越界方法
    private boolean outOfBound(int index){
        return index < 0 || index >= size;
    }
    //判断集合是否已满
    private boolean isFull(){return size == length;}
    //打印集合中的有效元素
    @Override
    public String toString() {
        StringBuilder out = new StringBuilder("[");
        for (int i = 0; i < size; i++) {
            out.append(objects[i]+",");
        }
        out.deleteCharAt(out.length()-1);
        out = out.append("]");
        return out.toString();
    }
}